<template>
	<sc-dialog v-model="visible" show-fullscreen destroy-on-close :title="titleMap[mode]" width="750px" @close="close">
		<el-form ref="formRef" label-width="100px" :model="formData" :rules="rules">
			<el-form-item label="警报设备" prop="equipName">
				<el-select v-model="formData.equipName" class="m-2" placeholder="请选择警报设备">
					<el-option
						v-for="item in alarmEquipNameOptions"
						:key="item.code"
						:label="item.name"
						:value="item.code" />
				</el-select>
			</el-form-item>
			<el-form-item label="警报分类" prop="classify">
				<el-select v-model="formData.classify" class="m-2" placeholder="请选择警报分类">
					<el-option
						v-for="item in alarmClassifyOptions"
						:key="item.code"
						:label="item.name"
						:value="item.code" />
				</el-select>
			</el-form-item>
			<el-form-item label="顺序号" prop="pos">
				<el-input-number v-model="formData.pos" :min="1" :max="100" />
			</el-form-item>
			<el-form-item label="警报内容" prop="content">
				<el-input
					v-model.trim="formData.content"
					placeholder="请输入警报内容"
					:maxlength="64"
					show-word-limit
					clearable>
				</el-input>
			</el-form-item>
			<el-form-item label="警报等级" prop="level">
				<el-radio-group v-model="formData.level">
					<el-radio :label="5">信息</el-radio>
					<el-radio :label="4">警告</el-radio>
					<el-radio :label="3">一般</el-radio>
					<el-radio :label="2">严重</el-radio>
					<el-radio :label="1">灾难</el-radio>
				</el-radio-group>
			</el-form-item>
		</el-form>

		<template #footer>
			<el-button @click="close">取 消</el-button>
			<el-button :loading="isSaveing" type="primary" @click="save">确 定</el-button>
		</template>
	</sc-dialog>
</template>

<script>
export default {
	emits: ['complete'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: "新增",
				edit: "编辑",
			},
			isSaveing: false,
			visible: false,
			alarmEquipNameOptions: [],
			alarmClassifyOptions: [],
			formData: {
				id: 0,
				equipName: '',
				content: '',
				pos: 1,
				level: 5,
				classify: '',
			},
			rules: {
				content: [{ required: true, message: "请输入警报内容", trigger: "blur" }],
				pos: [{ required: true, message: "请输入顺序号，从 1 开始", trigger: "blur" }],
				level: [{ required: true, message: "请选择警报等级", trigger: "blur" }],
				classify: [{ required: true, message: "请选择警报分类", trigger: "blur" }],
			},
		};
	},
	methods: {
		async open(row) {
			if (!row) {
				this.mode = "add";
			} else {
				this.mode = "edit";
				const resp = await this.$API.equipalarmitem.model.get(row.id);
				this.formData = resp.data;
			}

			const resp2 = await this.$API.syscodetype.bycode.get('mes_alarm_equipment');
			this.alarmEquipNameOptions = resp2.data.codes.map(s => ({ name: s.name, code: s.codeValues }));

			const resp3 = await this.$API.syscodetype.bycode.get('mes_alarm_classify');
			this.alarmClassifyOptions = resp3.data.codes.map(s => ({ name: s.name, code: s.codeValues }));

			this.visible = true;
		},
		save() {
			this.$refs.formRef.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					let resp = null;
					if (this.formData.id === 0) {
						resp = await this.$API.equipalarmitem.add.post(this.formData);
					} else {
						resp = await this.$API.equipalarmitem.update.put(this.formData);
					}
					this.isSaveing = false;
					if (resp.code == 200) {
						this.$emit("complete");
						this.visible = false;
						this.$message.success("保存成功");
					} else {
						this.$alert(resp.message, "提示", { type: "error" });
					}
				}
			});
		},
		close() {
			this.formData = {
				id: 0,
				equipName: '',
				content: '',
				pos: 1,
				level: 5,
				classify: '',
			};
			this.$refs.formRef.resetFields();
			this.visible = false;
		},
	},
};
</script>
